package cn.itsource.order.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.order.domain.Product;
import cn.itsource.order.query.ProductQuery;
import cn.itsource.order.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    /**
     * 查询所有
     */
    @GetMapping
    public AjaxResult queryAll() {
        List<Product> list = productService.queryAll();
        return AjaxResult.ok(list);
    }

    @GetMapping("{id}")
    public AjaxResult find(@PathVariable Long id){
        Product product = productService.find(id);
        return AjaxResult.ok(product);
    }

    /**
     * 分页查询
     */
    @GetMapping("/queryPage")
    public AjaxResult queryPage(ProductQuery query) {
        PageList<Product> list = productService.queryPage(query);
        return AjaxResult.ok(list);
    }

    /**
     * 保存数据
     */
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Product product) {
        boolean result = productService.save(product);
        return AjaxResult.toAjax(result);
    }


    /**
     * 删除操作
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Long id) {
        boolean result = productService.delete(id);
        return AjaxResult.toAjax(result);
    }

    @DeleteMapping
    public AjaxResult delete(@RequestBody Long[] ids) {
        boolean result = productService.batchDelete(ids);
        return AjaxResult.toAjax(result);
    }

    @PostMapping("/onoff")
    public AjaxResult onoff(Long[] id,Long state){
        boolean result = productService.onoff(id,state);
        return AjaxResult.ok(result);
    }
}
